package in.muton.edushare.academics.alerts;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.components.messaging.api.SystemMessage;
import in.muton.edushare.wicket.models.CayenneList;

import java.util.List;

import org.apache.wicket.model.IModel;
import org.apache.wicket.protocol.http.RequestUtils;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.CourseInstancesDAO;
import com.edushare.common.dao.CoursesDAO;
import com.edushare.common.dao.DepartmentsDAO;
import com.edushare.common.dao.EnrolmentsDAO;
import com.edushare.common.dao.UserDeptLinkDAO;
import com.edushare.common.dao.UserRolesDAO;
import com.edushare.common.orm.ORMInterface;
import com.edushare.common.util.URLUtils;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Enrolments;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.UserDeptLink;
import com.edushare.common.vo.UserRoles;
import com.edushare.common.vo.Users;

public class CourseCodeAlert {
	private IModel<Courses> course;
	private ORMInterface orm;

	public CourseCodeAlert(IModel<Courses> course, ORMInterface orm) {
		this.course = course;
		this.orm = orm;
	}

	public void sendMessage() {
		CourseInstancesDAO cInstDAO = new CourseInstancesDAO(orm);
		CourseInstances cInst = cInstDAO.getActiveInst(course.getObject());
		if (cInst == null)
			return;
		String msg = msgStart + ApplicationConstants.APPLICATION_NAME
				+ msgPart2;
		msg += (course.getObject().getCourseName() + msgPart3);
		msg += (getURL() + msgPart4);
		EnrolmentsDAO enrolDAO = new EnrolmentsDAO(orm);
		List<Users> recipientList = enrolDAO.getUsersByRole(cInst,
				Enrolments.TEACHER);
		Departments dept = new CoursesDAO(orm)
				.getParentDept(course.getObject());
		recipientList.addAll(new UserDeptLinkDAO(orm).getUserByRole(dept,
				UserDeptLink.PRIMARY_ADMIN));
		if (recipientList.size() == 0) {
			Universities univ = new DepartmentsDAO(orm).getParent(dept);
			recipientList.addAll(new UserRolesDAO(orm).getUsersByRole(univ,
					UserRoles.ADMIN_ROLE));
		}

		new SystemMessage(EduShareSession.get().getSessionORMInterface(),
				EduShareSession.get().getLoggedInUser(), cInst, msg,
				new CayenneList<Users>(recipientList)).send();

	}

	private String getURL() {
		return RequestUtils.toAbsolutePath(URLConstants.COURSE_EDIT + "/"
				+ URLUtils.escapeSplChars(course.getObject().getCourseName()))
				+ "/" + (course.getObject().getCourseId());
	}

	String msgStart = "<strong>Course Reminder</strong> : <br/>"
			+ "Please update the course code and course credits for the following courses. Without these details,"
			+ "<span wicket:id=\"appName\">";

	String msgPart2 = "</span> will not be able to calculate students' grades."
			+ "<div style=\"padding:5px;margin-bottom:5px;border-bottom:1px solid #ccc;font-weight:bold;\">"
			+ "<div style=\"text-align:center;\">";

	String msgPart3 = "</div>" + "<div style=\"clear:both;\"></div>"
			+ "These details can be updated <a href=\"";

	String msgPart4 = "\">here</a>.</div>";

}
